package com.jeequan.jeepay.pay.router;

import cn.hutool.core.collection.CollectionUtil;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchPayPassage;
import com.jeequan.jeepay.core.entity.MchPayRoute;
import com.jeequan.jeepay.core.entity.PayInterfaceDefine;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.pay.model.MchAppConfigContext;
import com.jeequan.jeepay.pay.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.service.impl.MchPayPassageService;
import com.jeequan.jeepay.service.impl.MchPayRouteService;
import com.jeequan.jeepay.service.impl.PayInterfaceDefineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 支付路由服务
 *
 * @author wxj
 * 2024/10/24
 */
@Service
public class PayRouteService {

    @Autowired
    private MchPayPassageService mchPayPassageService;

    @Autowired
    private PayInterfaceDefineService payInterfaceDefineService;

    @Autowired
    private MchPayRouteService mchPayRouteService;

    /**
     * 路由支付接口
     *
     * @param mchAppConfigContext
     * @param bizRQ
     * @param wayCode
     * @return
     */
    public MchPayPassage route(MchAppConfigContext mchAppConfigContext, UnifiedOrderRQ bizRQ, String wayCode) {
        List<MchPayPassage> list = mchPayPassageService.list(MchPayPassage.gw()
                .eq(MchPayPassage::getMchNo, mchAppConfigContext.getMchNo())
                .eq(MchPayPassage::getAppId, mchAppConfigContext.getAppId())
                .eq(MchPayPassage::getState, CS.YES)
                .eq(MchPayPassage::getWayCode, wayCode)
        );
        if (list.isEmpty()) {
            return null;
        } else { // 返回一个可用通道
            Map<String, MchPayPassage> mchPayPassageMap = list.stream().collect(Collectors.toMap(MchPayPassage::getIfCode, a -> a));
            // 查询ifCode所有接口
            List<PayInterfaceDefine> interfaceDefines = payInterfaceDefineService
                    .list(PayInterfaceDefine.gw()
                            .select(PayInterfaceDefine::getIfCode, PayInterfaceDefine::getState)
                            .eq(PayInterfaceDefine::getState, CS.YES)
                            .in(PayInterfaceDefine::getIfCode, mchPayPassageMap.keySet()));

            if (CollectionUtil.isNotEmpty(interfaceDefines)) {
                MchPayRoute mchPayRoute = mchPayRouteService.getOne(MchPayRoute.gw()
                        .eq(MchPayRoute::getAppId, mchAppConfigContext.getAppId())
                        .eq(MchPayRoute::getWayCode, wayCode)
                        .eq(MchPayRoute::getState, CS.YES), false
                );
                if (mchPayRoute != null) {
                    Map<String, PassageInfo> channelInfoMap = new HashMap<>();
                    interfaceDefines.forEach(interfaceDefine -> {
                        PassageInfo passageInfo = new PassageInfo(mchPayPassageMap.get(interfaceDefine.getIfCode()), interfaceDefine);
                        channelInfoMap.put(interfaceDefine.getIfCode(), passageInfo);
                    });
                    RouteStrategy routeStrategy = SpringBeansUtil.getBean(mchPayRoute.getStrategy() + "RouteStrategy", RouteStrategy.class);
                    if (routeStrategy == null) {
                        throw new BizException("无此路由策略");
                    }
                    return routeStrategy.route(mchAppConfigContext, bizRQ, mchPayRoute, channelInfoMap);
                } else {
                    return mchPayPassageMap.get(interfaceDefines.get(0).getIfCode());
                }
            }
        }
        return null;
    }
}
